﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Application.Command.Add;
using Traceability.App.Domain;
using Traceability.App.ErrorCode;
using Traceability.App.Instructure;

namespace Traceability.App.Api.Application.Handler.Add
{
    public class DelBaoHandler : IRequestHandler<DelBaoCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<HealthreportModel> healthreportRepo;
        private readonly IMapper mapper;

        public DelBaoHandler(IBaseRepository<HealthreportModel> healthreportRepo,IMapper mapper) 
        {
            this.healthreportRepo = healthreportRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(DelBaoCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var bao = healthreportRepo.GetAll().Where(x => x.Id == request.Id).FirstOrDefault();
            bao = mapper.Map(request, bao);
            bao.IsDelete = true;
            var i = healthreportRepo.Update(bao);
            if (i>0)
            {
                res.Code = ApiEnum.Success;
                res.Data = i;
                res.Msg = "删除成功";
            }
            else
            {
                res.Code = ApiEnum.Error;
                res.Data = i;
                res.Msg = "删除失败";
            }
            return Task.FromResult(res);
        }
    }
}
